package com.tansty.service.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.tansty.common.utils.MD5Util;
import com.tansty.dao.mapper.UserMapper;
import com.tansty.pojo.dto.PasswordDTO;
import com.tansty.pojo.dto.UserDTO;
import com.tansty.pojo.po.Permission;
import com.tansty.pojo.po.Role;
import com.tansty.pojo.po.User;
import com.tansty.service.enums.Errors;
import com.tansty.service.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Tansty
 * @since 2021-07-31
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public List<Permission> findPermissionByUsername(String username) {
        return userMapper.findPermissionByUsername(username);
    }

    @Override
    public List<Role> findRoleByUsername(String username) {
        return userMapper.findRoleByUsername(username);
    }

    @Override
    public List<UserDTO> getAllUsers() {
        return userMapper.getAllUsers();
    }

    @Override
    public int deletedOne(int id) {
        return userMapper.deletedOne(id);
    }

    @Override
    public int updatePassword(PasswordDTO passwordDTO) {
        User user = userMapper.selectById(passwordDTO.getId());
        if(!MD5Util.encode(passwordDTO.getOldPassword()).equals(user.getPassword())){
            return Errors.OLD_PASSWORD_IS_NOT_MATCH.getCode();
        }
        user.setPassword(MD5Util.encode(passwordDTO.getNewPassword()));
        return   userMapper.updateById(user);
    }
}
